package com.google.code.head2head.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.code.head2head.dao.EntryDao;
import com.google.code.head2head.dao.GradeDao;
import com.google.code.head2head.dao.UserDao;
import com.google.code.head2head.exceptions.AlreadyRatedException;
import com.google.code.head2head.exceptions.RatingOwnedEntryException;
import com.google.code.head2head.model.Entry;
import com.google.code.head2head.model.Grade;
import com.google.code.head2head.model.WebUser;
import com.google.code.head2head.service.EntryService;

@Transactional(propagation = Propagation.REQUIRED)
public class EntryServiceImpl implements EntryService {

	private String entriesImagesDirectory;

	private EntryDao entryDao;

	private GradeDao gradeDao;
	
	private UserDao userDao;

	public void create(Entry entry) {

		// persists the entry
		entryDao.save(entry);
	}

	public void update(Entry entry) {
		entryDao.update(entry);
	}

	public List<Entry> findAll(int first, int last) {
		return entryDao.findAll(first, last);
	}

	public List<Entry> findAll() {
		return entryDao.findAll();
	}

	public List<Entry> findEntriesWithTag(int first, int count, String... tags) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Entry> findEntriesWithTag(String... tags) {
		return entryDao.getEntriesWithTags(tags);
	}

	public Entry getEntry(Long id) {
		// TODO Auto-generated method stub
		return entryDao.getEntity(id);
	}

	public Entry getEntryWithRelations(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public EntryDao getEntryDao() {
		return entryDao;
	}

	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}

	public String getEntriesImagesDirectory() {
		return entriesImagesDirectory;
	}

	public void setEntriesImagesDirectory(String entriesImagesDirectory) {
		this.entriesImagesDirectory = entriesImagesDirectory;
	}

	public void rateEntry(Long entryId, Long userId, int rate, String ipAdress) throws AlreadyRatedException, RatingOwnedEntryException{
		Entry entry = entryDao.getEntity(entryId);
		for (Grade grade : entry.getGrades()) {
			if(grade.getIp().equals(ipAdress)){
				throw new AlreadyRatedException();
			}
		}
		
		for(String ip: entry.getIps()){
			if(ip.equals(ipAdress)){
				throw new RatingOwnedEntryException();
			}
		}
		entry.getIps().add(ipAdress);
		Grade grade = new Grade();
		grade.setGrade(rate);
		grade.setEntry(entry);
		grade.setIp(ipAdress);

		if (userId != null) {
			WebUser user=userDao.getEntity(userId);
			grade.setUser(user);
		}

		gradeDao.save(grade);
	}

	public GradeDao getGradeDao() {
		return gradeDao;
	}

	public void setGradeDao(GradeDao gradeDao) {
		this.gradeDao = gradeDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public Entry getEntryWithGrades(Long id) {
		// TODO Auto-generated method stub
		return entryDao.getEntryWithGrades(id);
	}

}
